package com.example.chatchatbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.chatchatbackend.entity.dto.Adv;

import com.example.chatchatbackend.entity.vo.request.AdvVO;
import com.example.chatchatbackend.mapper.AdvMapper;
import com.example.chatchatbackend.service.AdvService;

import com.example.chatchatbackend.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Service
@Slf4j
public class AdvServiceImpl extends ServiceImpl<AdvMapper, Adv> implements AdvService {

    private static final LocalDateTime currentTime  = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private AdvMapper advMapper;

    @Override
    public List<Adv> getAllAdv() {
        return list();
    }

    @Override
    public AdvVO insertAdv(AdvVO advVo) {
        LocalDateTime now =LocalDateTime.now();
        Adv adv = new Adv(null,advVo.getTitle(),advVo.getContent(),this.currentTime,this.currentTime,advVo.getImageUrl());
        if (this.save(adv))
            return advVo;
        return null;
    }

    @Override
    public List<Adv> insertAdvList(List<Adv> advs) {
        for (Adv adv : advs) {
            adv.setCreatedTime(this.currentTime);
        }
        advMapper.insertList(advs);
        return advs;
    }



    @Override
    public IPage<Adv> pageThree(Integer pageNum, Integer pageSize) {
        Page<Adv> page = new Page<>(pageNum, pageSize);
        IPage<Adv> advIPage = advMapper.selectPage(page, null);
        return advIPage;
    }
    @Override
    public AdvVO updateAdv(AdvVO advVO) {
        Adv adv = new Adv();
        BeanUtils.copyProperties(advVO, adv);
        adv.setUpdateTime(this.currentTime);
        advMapper.updateAdv(adv);
        return advVO;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        advMapper.deleteBatchIds(ids);
    }

    @Override
    public List<AdvVO> findByIds(List<Integer> ids) {
        List<Adv> advList = advMapper.selectBatchIds(ids);
        List<AdvVO> advVOList = new ArrayList<>();

        for (Adv adv : advList) {
            AdvVO advVO = new AdvVO();
            BeanUtils.copyProperties(adv, advVO);
            advVOList.add(advVO);  // 添加到结果列表
        }

        return advVOList;
    }

    @Override
    public List<AdvVO> findByTitle(String title) {

        LambdaQueryWrapper<Adv> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(StringUtils.hasText(title), Adv::getTitle, title);
        List<Adv> advList = advMapper.selectList(wrapper);
        log.info("{}",advList);

        List<AdvVO> advVOList = new ArrayList<>();

        for (Adv adv : advList) {
            AdvVO advVO = new AdvVO();
            BeanUtils.copyProperties(adv, advVO);
            advVOList.add(advVO);  // 添加到结果列表
        }
        return advVOList;
    }



}
